Ext.onReady(function()
    {
        /*var columnrender = function(val, meta, rec, ri, ci, s) {
            if (val=='1'){
                return 'Masculino';
            }else if (val=='2'){
                return 'Femenino';
            }
        }
        // HANDLE AJAX
        var h_obtener_success = function(obj){

            var grid = X.get('editConfigGrid');
            grid.stopEditing();
           
            for ( var i=0; i<obj.rows.length; i++){
                var defaultData = {
                    idPerfil:obj.rows[i]['idPerfil'],
                    idDepartamento:obj.rows[i]['idDepartamento']
                };
                var p = new grid.store.recordType(defaultData);
              grid.store.add(p);
            }
            grid.getSelectionModel().selectRow(0);
      }

        var h_obtener_success2 = function(obj){
            

             var grid = X.get('editConfigGrid');
             var str = null;
             var len= grid.store.getCount();

            grid.stopEditing();
            for ( var i=len; i<(obj.rows.length+len); i++){
             
                var defaultData2 = {
                    idPerfil:obj.rows[i-len]['idPerfil'],
                    idDepartamento:str
                };
           
                var p = new grid.store.recordType(defaultData2);
              grid.store.add(p);

      }
           grid.getSelectionModel().selectRow(0);
//
        }*/
        // HANDLE CONFIRM
        var h_confirmar = function(row){
            X.ajax({
                url     : '/calendario/ajax-eliminar',
                params  : {periodo : row.get('periodo')},
                afterSuccess : function (){
                    //X.reload('listGrid');
                    window.location = '/calendario';
                }
            });
        }
        //HANDLE IMPORTAR
        var h_importar = function(row){
            var year = row.get('periodo');
            X.get('importAnho').setValue(year);
            X.get('importAnho').disable();
            var importStore = X.get('importStore');
            var listStore = X.get('listStore');
            var n = listStore.getCount();
            var r, recordYear;
            for (var i=0;i<n;i++){
                r=listStore.getAt(i);
                recordYear = r.get('fechaInicio').slice(0,4);
                if(year == recordYear){
                    importStore.add(r.copy());
                }
            }
            X.get('importWindow').begin();
        }

        // HANDLE ACTION
        var h_editar = function(row){
            var year = row.get('periodo');
            X.get('editAnho').setValue(year);
            X.get('editAnho').disable();
            var editStore = X.get('editStore');
            var listStore = X.get('listStore');
            var n = listStore.getCount();
            var r, recordYear;
            for (var i=0;i<n;i++){
                r=listStore.getAt(i);
		  //alert(r.toSource());
                recordYear = r.get('fechaInicio').slice(0,4);
		  //recordYear = r.get('fechaInicio').date;
                //recordYear = recordYear.slice(0,4);
		//  alert(recordYear);
                if(year == recordYear){
                    /*var val;
                    try{
                        val = Date.parseDate(r.get('fechaInicio'), 'Y-m-d').format('d/m/Y');
                    }catch(e){ try{
                        val = Date.parseDate(r.get('fechaInicio'), 'Y-m-d H:i:s').format('d/m/Y');
                    }catch(e){ try{
                        val = r.get('fechaInicio').format('d/m/Y');
                    }catch(e){
                        val = r.get('fechaInicio');
                    }}}

                    if (typeof val == 'string'){
                        //alert('here');
                        val = val.replace(/"/g,'\\"');
                    }
                    //alert('val: '+val+'del type '+typeof(val));
                    r.set('fechaInicio', val);*/
                    editStore.add(r.copy());
		    //  alert('done');
                }
            }
            X.get('editWindow').begin();
        }
        
        var h_eliminar = function(row){
            X.msg.confirm('Está seguro de eliminar el calendario del año "'+row.get('periodo')+'"?',
                X.call(h_confirmar, row));
        }

        // STORE
        X.store({
            id     : 'listStore',
            url    : '/calendario/ajax-listar',
            fields : [ 'idCalendario', 'descripcion', 'fechaInicio', 'fechaFin',
                   'horaInicio', 'horaFin', 'tipo', 'carrera', 'semestres' ]
            /*fields : [ 'idCalendario', 'descripcion', {name:'fechaInicio', type:'date'},
                   {name:'fechaFin', type:'date'},'horaInicio','horaFin', 'tipo', 'carrera', 'semestres' ]*/
        });

        X.store({
            id     : 'yearStore',
            fields : [ 'idCalendario', 'periodo' ]
        });

        // GRID
        X.grid.panel({
            id       : 'listGrid',
            title    : 'Lista de Calendarios',
            //renderTo : 'layout_content',
            disableSelection: true,
            height   : 372,
            width    : 162,
            storeId  : 'yearStore',
            tbar : new Ext.Toolbar({
                items:[
                    X.form.button({
                        text:'Crear Calendario',
                        id:'listCrear',
                        icon:'/img/icon_agregar.png'
                    }),{
                        xtype: 'tbseparator', id:'listEspacio'
                    }
                ]
            }),
            columns:[
            X.grid.text  ({
                header: 'Periodo',
                dataIndex: 'periodo',
                width: 57
            }),
            X.grid.Import({
               handler : h_importar
            }),
            X.grid.edit  ({
                handler : h_editar
            }),
            X.grid.remove({
                handler : h_eliminar
            })
            ]/*,
            bbar : X.grid.paging({
                pageSize : 20,
                storeId  : 'yearStore'
            })*/
        });

        // EVENT
        X.event.button('listCrear', function(){
            X.get('createWindow').begin();
        });
   

X.win.panel({
        id       : 'listWindow',
        //title    : 'Carga de horario',
        modal    : false,
        resizable: false,
        border: false,
        items    : X.get('listGrid')
    });

    X.get('listWindow').show();

});